﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AD_Project.Models;

namespace AD_Project.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Account/
        EpShareEntities _db = new EpShareEntities();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Folder()
        {
            return View();
        }

        public ActionResult Info()
        {
            return View();
        }

        public ActionResult FolderInfo()
        {
            return PartialView("_FolderInfo");
        }

        public ActionResult DownloadHistory()
        {
            return View();
        }

        public ActionResult LoginModal()
        {
            return PartialView("_Login");
        }

        public ActionResult MyFile()
        {
            return View();
        }

        [HttpPost]
        public string AjaxLogin(string username, string password)
        {
            //// Insert object
            //Staff staff = new Staff();
            //staff.account = "AAA";
            //staff.staff_name = "BBB";
            ////Add more values
            //_db.Staffs.Add(staff);
            //_db.SaveChanges();

            ////List
            //var items = _db.Staffs.ToList(); // return null if no values
            //var list1 = _db.Staffs.Where(st => st.staff_name.Contains("B")).ToList();
            //var list2 = _db.Staffs.Where(st => st.staff_name.Contains("B")).Single();

            //var object1 = _db.Staffs.Single(st => st.staff_name.Contains("B"));


            ////delete
            //_db.Staffs.Remove(object1);
            if (Request.IsAjaxRequest() == true)
            {
                try
                {
                    var result = _db.Accounts.Single(a => a.username.Equals(username) && a.password.Equals(password));

                    Session["acc"] = result;                                    
                }
                catch
                {
                    // Bad request
                    Response.StatusCode = 401;
                    return "Error";
                }
                return "OK";   
            }
            else
            {
                return "ABC";
            }          
        }

        public ActionResult _Menu()
        {
            return View();
        }
    }
}
